package com.cy.xrd.sms.service.impl;

import com.cy.common.exception.Assert;
import com.cy.common.exception.BusinessException;
import com.cy.common.result.ResponseEnum;
import com.cy.xrd.sms.service.SmsService;
import com.cy.xrd.sms.util.SmsProperties;
import com.google.gson.Gson;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @title: SmsServiceImpl
 * @Author Luo
 * @Date: 2021/7/14 下午 01:03
 * @Version 1.0
 */
@Slf4j
@Service
public class SmsServiceImpl implements SmsService {
    @Override
    public void send(String mobile, String templateCode, String param) {
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(SmsProperties.KEY_ID, SmsProperties.KEY_SECRET);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumber = {"+86" + mobile};
            req.setPhoneNumberSet(phoneNumber);
            req.setSmsSdkAppid(SmsProperties.APP_ID);
            req.setSign(SmsProperties.SIGN_NAME);
            req.setTemplateID(templateCode);
            Gson gson = new Gson();
            String json = gson.toJson(param);
            String[] templateParam = {param};
            req.setTemplateParamSet(templateParam);

            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            boolean success = resp.any().isEmpty();
            Assert.isTrue(success, ResponseEnum.TENCENTCLOUD_RESPONSE_FAIL);
            SendStatus[] data = resp.getSendStatusSet();
            String data0 = SendStatus.toJsonString(data[0]);
            System.out.println(data0);
            HashMap<String, String> resultMap = gson.fromJson(data0, HashMap.class);
            String code = resultMap.get("Code");
            String message = resultMap.get("Message");
            log.info("腾讯云短信发送响应结果：");
            log.info("code：" + code);
            log.info("message：" + message);
            boolean limit = code.contains("limit");
            Assert.notEquals(limit, code, ResponseEnum.TENCENTCLOUD_SMS_LIMIT_CONTROL_ERROR);
            Assert.equals("Ok", code, ResponseEnum.TENCENTCLOUD_SMS_ERROR);

        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
            throw new BusinessException(ResponseEnum.TENCENTCLOUD_SMS_ERROR);
        }
    }
}

